<?php

$rid = $_GET['id'];
$bstrURL = "https://mp.huya.com/cache.php?m=Live&do=profileRoom&roomid=$rid";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$bstrURL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
curl_close($ch);

$json = json_decode($data);
$bStreamLst = $json->data->stream->baseSteamInfoList[0];

$sStreamName = $bStreamLst->sStreamName;
$sHlsUrl = $bStreamLst->sHlsUrl;
$sAntiCode = $bStreamLst->sHlsAntiCode;

parse_str($sAntiCode,$params);
$fm = base64_decode($params['fm']);
$wsTime = $params['wsTime'];
$ctype = $params['ctype'];
$seqid = time().'000'; // uid(0) + date.now() == 13位时间戳
$i = md5($seqid.'|'.$ctype.'|100'); // t = 100 若为动态请从AntiCode获取

$wsSecret = md5(str_replace(['$0','$1','$2','$3'],['0',$sStreamName,$i,$wsTime],$fm));  // uid_streamname_hash_wstime

$szURL = $sHlsUrl.'/'.$sStreamName.'.m3u8?wsSecret='.$wsSecret.'&wsTime='.$wsTime.'&uid=0&fm='.urlencode(base64_encode($fm)).'&ctype='.$ctype.'&seqid='.$seqid.'&ver=1&t=100';

header('location:'.$szURL);
?>